﻿using System;
using System.Linq;
using Infrastructure;
using SnowTech.SmartTms.App.Interface;
using SnowTech.SmartTms.App.Request;
using SnowTech.SmartTms.App.Response;
using SnowTech.SmartTms.Repository;
using SnowTech.SmartTms.Repository.Domain;
using SnowTech.SmartTms.Repository.Interface;
namespace SnowTech.SmartTms.App
{
    public class suppliespurchasingApp : BaseStringApp<suppliespurchasing, OpenAuthDBContext>
    {
        private RevelanceManagerApp _revelanceApp;
        /// <summary>
        /// 加载列表
        /// </summary>
        public TableData Load(QuerysuppliespurchasingListReq request)
        {
            var loginContext = _auth.GetCurrentUser();
            if (loginContext == null)
            {
                throw new CommonException("登录已过期", Define.INVALID_TOKEN);
            }
           //如果是WebAPI,请务必改为loginContext.GetTableColumns("suppliespurchasing");
           var columnFields = loginContext.GetTableColumnsFromDb("suppliespurchasing");
            if (columnFields == null || columnFields.Count == 0)
            {
                throw new Exception("请在代码生成界面配置Category表的字段属性");
            }
            var result = new TableData();
            var objs = UnitWork.Find<suppliespurchasing>(null);
            if (!string.IsNullOrEmpty(request.key))
            {
                objs = objs.Where(u => u.Id.Contains(request.key));
            }
             var propertyStr = string.Join(',', columnFields.Select(u =>u.ColumnName));
            result.columnFields = columnFields;
            result.data = objs.OrderBy(u => u.Id)
                .Skip((request.page - 1) * request.limit)
                .Take(request.limit).Select($"new ({propertyStr})");
            result.count = objs.Count();
            return result;
        }
        public void Add(AddOrUpdatesuppliespurchasingReq req)
        {
            var obj = req.MapTo<suppliespurchasing>();
            //todo:根据自己的业务场景，补充或调整字段
            //比如：obj.CreateTime = DateTime.Now;
            // var user = _auth.GetCurrentUser().User;
            // obj.CreateUserId = user.Id;
            // obj.CreateUserName = user.Name;
            Repository.Add(obj);
        }
         public void Update(AddOrUpdatesuppliespurchasingReq obj)
        {
            UnitWork.Update<suppliespurchasing>(u => u.Id == obj.Id, u => new suppliespurchasing
            {
                SuppliesName = obj.SuppliesName, 
                SCount = obj.SCount, 
                SLastDate = obj.SLastDate, 
                SRemarks = obj.SRemarks, 
                CreateDate = obj.CreateDate, 
                CreateUserId = obj.CreateUserId, 
                CreateUserName = obj.CreateUserName, 
                FlowInstanceId = obj.FlowInstanceId
                //todo:根据自己的业务场景，补充或调整字段
            });
        }
        public suppliespurchasingApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<suppliespurchasing, OpenAuthDBContext> repository,
            RevelanceManagerApp app, IAuth auth) : base(unitWork, repository,auth)
        {
            _revelanceApp = app;
        }
    }
}
